/**
  * @File    :   veml6075.c
  * @Time    :   2020/04/22 22:23:34
  * @Author  :   huenrong
  */

#include "./veml6075.h"

/**
  * @name   veml6075_read_device_id
  * @brief  veml6075读取设备id
  * @param  read_id: 读取到的设备id
  * @param  fd: 设备文件描述符
  * @retval 成功: 0
  *         失败: -1
  */
int veml6075_read_device_id(uint16_t *read_id, const int fd)
{
    int ret = -1;
    uint8_t read_data[3] = {0};

    ret = i2c_read_data_sub(read_data, fd, VEML6075_I2C_ADDR, VEML6075_DEVICE_ID, 2);
    if (2 != ret)
    {
        printf("veml6075 read device id fail\n");

        return -1;
    }

    (*read_id) = ((read_data[1] << 8) | read_data[0]);

    return 0;
}

/**
  * @name   veml6075_shutdown
  * @brief  关闭veml6075
  * @param  fd: 设备文件描述符
  * @retval 成功: 0
  *         失败: -1
  */
int veml6075_shutdown(const int fd)
{
    int ret = -1;
    uint8_t veml6075_config = 0;

    veml6075_config = (VEML6075_CONF_UV_IT_100MS | VEML6075_CONF_SD);

    ret = i2c_write_data_sub(fd, VEML6075_I2C_ADDR, VEML6075_UV_CONF, &veml6075_config, 1);
    if (-1 == ret)
    {
        printf("veml6075 shutdown fail\n");

        return -1;
    }

    return 0;
}

/**
  * @name   veml6075_enable
  * @brief  使能veml6075
  * @param  fd: 设备文件描述符
  * @retval 成功: 0
  *         失败: -1
  */
int veml6075_enable(const int fd)
{
    int ret = -1;
    uint8_t veml6075_config = 0;

    veml6075_config = VEML6075_CONF_UV_IT_100MS;

    ret = i2c_write_data_sub(fd, VEML6075_I2C_ADDR, VEML6075_UV_CONF, &veml6075_config, 1);
    if (-1 == ret)
    {
        printf("veml6075 enable fail\n");

        return -1;
    }

    return 0;
}

/**
  * @name   veml6075_trig
  * @brief  触发veml6075
  * @param  fd: 设备文件描述符
  * @retval 成功: 0
  *         失败: -1
  */
int veml6075_trig(const int fd)
{
    int ret = -1;
    uint8_t veml6075_config = 0;

    veml6075_config = (VEML6075_CONF_UV_IT_100MS | VEML6075_CONF_UV_TRIG_ONCE | VEML6075_CONF_HD);

    ret = i2c_write_data_sub(fd, VEML6075_I2C_ADDR, VEML6075_UV_CONF, &veml6075_config, 1);
    if (-1 == ret)
    {
        printf("veml6075 trig fail\n");

        return -1;
    }

    return 0;
}

/**
  * @name   veml6075_init
  * @brief  veml6075初始化
  * @param  fd: 设备文件描述符
  * @retval 成功: 返回0
            失败: 返回-1
  */
int veml6075_init(const int fd)
{
    int ret = -1;
    uint16_t veml6075_device_id = 0;

    ret = veml6075_enable(fd);
    if (0 != ret)
    {
        printf("veml6075 enable fail\n");

        return -1;
    }

    // 读取veml6075设备ID
    ret = veml6075_read_device_id(&veml6075_device_id, fd);
    if (0 != ret)
    {
        printf("read veml6075 device id fail\n");

        return -1;
    }

    // 设备ID正确
    if (0x0026 == veml6075_device_id)
    {
        return 0;
    }

    return -1;
}

/**
  * @name   veml6075_read_uva
  * @brief  veml6075读取uva值
  * @param  read_uva: 读取到的uva值(结果为0.01 uW/cm2)
  * @param  fd: 设备文件描述符
  * @retval 成功: 0
  *         失败: -1
  */
int veml6075_read_uva(uint16_t *read_uva, const int fd)
{
    int ret = -1;
    uint32_t uva = 0, uv_comp1 = 0, uv_comp2 = 0;
    uint8_t read_data[3] = {0};

    ret = i2c_read_data_sub(read_data, fd, VEML6075_I2C_ADDR, VEML6075_UVA_DATA, 2);
    if (2 != ret)
    {
        return -1;
    }
    uva = ((read_data[1] << 8) | read_data[0]);
    uva *= 100;

    memset(read_data, 0, sizeof(read_data));
    ret = i2c_read_data_sub(read_data, fd, VEML6075_I2C_ADDR, VEML6075_UVCOMP1_DATA, 2);
    if (2 != ret)
    {
        return -1;
    }
    uv_comp1 = ((read_data[1] << 8) | read_data[0]);
    uv_comp1 *= 222;

    memset(read_data, 0, sizeof(read_data));
    ret = i2c_read_data_sub(read_data, fd, VEML6075_I2C_ADDR, VEML6075_UVCOMP2_DATA, 2);
    if (2 != ret)
    {
        return -1;
    }
    uv_comp2 = ((read_data[1] << 8) | read_data[0]);
    uv_comp2 *= 133;

    uv_comp1 += uv_comp2;
    if (uva > uv_comp1)
    {
        uva -= uv_comp1;
    }
    else
    {
        uva = 0;
    }

    uva *= 44; // 校准系数0.44, 根据测试值计算
    uva /= 100;

    // 结果为0.01 uW/cm2
    *read_uva = (uint16_t)uva;

    return 0;
}
